/**
  ******************************************************************************
  * @file    User/user.c
  * @author  JUSHENG Application Team
  * @version V1.0.4
  * @date    12-14-2021
  * @brief   Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2021 JUSHENG</center></h2>
  *
  *
  ******************************************************************************
  */ 

/* Includes ------------------------------------------------------------------*/
#include "include.h"
#include "user.h"
#include "tk_cfg.h"

/** @addtogroup Template_Project
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
u8 Rx_Data;
u8 Spi_Buf[3] = {0x12, 0x30, 0x13};
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
 
void Spi_Slave_Init(void)
{
    TYPE_LL_GPIO_INIT  gpio_init;
    TYPE_LL_SPI_INIT spi_init;

    memset(&gpio_init, 0x00, sizeof(gpio_init));
    memset(&spi_init, 0x00, sizeof(spi_init));
    /* FUN1  PA2 SPI0_NSS, PA3 SPI0_CLK, PA4 SPI0_IO0, PA5 SPI0_IO1 */
    gpio_init.gpio_mode  = LL_GPIO_MODE_AF;
    gpio_init.gpio_pin   = LL_GPIO_PIN_2 | LL_GPIO_PIN_3 | LL_GPIO_PIN_4 | LL_GPIO_PIN_5;
    gpio_init.gpio_speed_level = LL_GPIO_SPEED_LEVEL_1;
    ll_gpio_init(GPIOA, &gpio_init);
    ll_gpio_pin_af_config(GPIOA, LL_GPIO_PIN_SOURCE_2, LL_GPIO_AF_1);
    ll_gpio_pin_af_config(GPIOA, LL_GPIO_PIN_SOURCE_3, LL_GPIO_AF_1);
    ll_gpio_pin_af_config(GPIOA, LL_GPIO_PIN_SOURCE_4, LL_GPIO_AF_1);
    ll_gpio_pin_af_config(GPIOA, LL_GPIO_PIN_SOURCE_5, LL_GPIO_AF_1);

    spi_init.work_mode             = LL_SSP_SLAVE_MODE;
    spi_init.delay_cycle_cnt       = 0;
    spi_init.baud                  = 1000;
    spi_init.cs_en                 = ENABLE;
    spi_init.cs_rising_ie          = DISABLE;
    spi_init.frame_ie              = DISABLE;
    spi_init.fifo_ov_ie            = DISABLE;
    spi_init.rfifo_not_empty_ie    = DISABLE;
    spi_init.tfifo_not_full_ie     = DISABLE;
    spi_init.frame_size            = LL_SPI_FRAME_8_BIT;
    spi_init.wire_mode             = LL_SPI_NORMAL_MODE;
    spi_init.spi_mode              = LL_SPI_MODE_0;
    ll_spi_init(SPI0, &spi_init);
}

/** 
  * @brief  Spi slave tx byte
  * @param  p_spi: The structure pointer of the SPI is selected
  * @param  *buf : Start address pointer
  * @param  num : Received byte length
  * @retval None
  */
void Spi_Slave_Tx_Byte(SPI_IIC_TypeDef *p_spi, u8 *buf, u32 num)
{   
    while(LL_SPI_IIC_STA_SLAVE_CS_STATE_GET(p_spi));
    ll_spi_iic_tx_enable(p_spi);

    while(num--) 
    {
        while(LL_SPI_IIC_FIFO_FULL_PEND_GET(p_spi));
        p_spi->CMD_DATA = *buf++;
    }

    while(!(LL_SPI_IIC_FIFO_EMPTY_PEND_GET(p_spi)));
    while(LL_SPI_IIC_BUSY_PEND_GET(p_spi));
}

/** 
  * @brief  Spi slave rx byte
  * @param  None
  * @retval None
  */
void Spi_Slave_Rx_Byte(void)
{   
    while(LL_SPI_IIC_STA_SLAVE_CS_STATE_GET(SPI0));
    ll_spi_iic_rx_enable(SPI0);
    
    while(LL_SPI_IIC_FIFO_EMPTY_PEND_GET(SPI0));
    Rx_Data = SPI0->CMD_DATA;
    SPI0->CMD_DATA = 0x00;
    
    Spi_Slave_Tx_Byte(SPI0, Spi_Buf, 3);
}


/**
  * @}
  */

/*************************** (C) COPYRIGHT 2021 JUSHENG ***** END OF FILE *****/
